package com.adb.androidwebalbum.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.adb.androidwebalbum.R;
import com.adb.androidwebalbum.bean.ImageBean;
import com.adb.androidwebalbum.uc.CustomBottomBar;
import com.adb.androidwebalbum.uc.CustomTopMenu;
import com.adb.androidwebalbum.uc.OnHeadNavigationSelectedListener;
import com.adb.androidwebalbum.utils.Const;
import com.adb.androidwebalbum.utils.Log;
import com.adb.androidwebalbum.utils.Utility;
import com.nostra13.universalimageloader.core.DisplayImageOptions;


public class ImageDetailActivity extends BaseActivity implements OnHeadNavigationSelectedListener,OnClickListener{
	
	private static String TAG = ImageDetailActivity.class.getCanonicalName();
	
	public ArrayList<ImageBean> arrListImageBean;
	private CustomTopMenu mCustomTopMenu;
	private CustomBottomBar mCustomBottomBar;
	
	public static final String EXTRA_IMAGE = "extra_image";
	
	private ImagePagerAdapter mAdapter;
	private ViewPager mPager;
	
	public DisplayImageOptions mOptions;
	
	public int Index = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    Log.debug(TAG,"[ onCreate ] ");
	    setContentView(R.layout.image_detail_pager);
	    Const.CONTEXT = ImageDetailActivity.this;
	    
	    mCustomTopMenu = new CustomTopMenu(this,getLayoutInflater());
		mCustomBottomBar = new CustomBottomBar(this,getLayoutInflater());
		
			
		arrListImageBean = new ArrayList<ImageBean>();
		for(ImageBean mBean : Utility.readObject(Const.FILE_PHOTOS_LIST)){
			arrListImageBean.add(mBean);
		}
		
		mOptions = new DisplayImageOptions.Builder()
		.showImageForEmptyUri(R.drawable.icon_menu_refresh)
		.cacheInMemory()
		.cacheOnDisc()
		.build();
		
		
		// Set up ViewPager and backing adapter
		mAdapter = new ImagePagerAdapter(getSupportFragmentManager(),
				arrListImageBean.size());
	
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);
//		mPager.setPageMargin((int) getResources().getDimension(R.dimen.image_detail_pager_margin));
		
		mPager.setOnPageChangeListener(new OnPageChangeListener() 
		{
		
			public void onPageSelected(int position) {
				Index = position;
				//UPDATE TOP MENU
				mCustomTopMenu.setPageSize(Index, arrListImageBean);
				mCustomBottomBar.setNavigationControl(Index, arrListImageBean);
			}
			
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		
		// Set the current item based on the extra passed in to this activity
	    final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1);
	    if (extraCurrentItem != -1) {
	    	Index = extraCurrentItem;
	        mPager.setCurrentItem(Index);
	    }
	    
	    final Object data = getLastCustomNonConfigurationInstance();
	    if(data != null){
	    	Index =(Integer)data;
	    	Log.debug(TAG," getLastCustomNonConfigurationInstance" + Index);
	    	mPager.setCurrentItem(Index);
	    	
	    }
	
	    
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.debug(TAG,"[ onSaveInstanceState ] ");
		outState.putInt(EXTRA_IMAGE,Index);
	}
	
	
	/** 
	 * When Orientation change keep Index value 
	 */
	@Override
	public Object onRetainCustomNonConfigurationInstance() {
		return Index;
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.debug(TAG,"[ onResume ] ");
		
		// show popup window when activity start 
		PerformMenu();
		
		//UPDATE TOP MENU
		mCustomBottomBar.setNavigationControl(Index,arrListImageBean);
		// UPDATE TOP MENU
		mCustomTopMenu.setPageSize(Index, arrListImageBean);
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.debug(TAG,"[ onStart ] ");
		
	
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.debug(TAG,"[ onDestroy ] ");
		setResult(RESULT_OK,new Intent().putExtra(Const.INTENT_POSITION,Index+""));
		hidingMenu();
		
		
	}
	
	private class ImagePagerAdapter extends FragmentStatePagerAdapter {
	    private final int mSize;
	
	    public ImagePagerAdapter(FragmentManager fm, int size) {
	        super(fm);
	        mSize = size;
	    }
	
	    @Override
	    public int getCount() {
	        return mSize;
	    }
	
	    @Override
	    public Fragment getItem(int position) {
	    	return ImageDetailFragment.newInstance(position);
	    }
	
	    @Override
	    public void destroyItem(ViewGroup container, int position, Object object) {
	        final ImageDetailFragment fragment = (ImageDetailFragment) object;
	        // As the item gets destroyed we try and cancel any existing work.
	        fragment.cancelWork();
	        super.destroyItem(container, position, object);
	    }
	    
	}
	
	public ArrayList<ImageBean> getLatestArrayImageBean(){
		
		if(arrListImageBean != null){
			return arrListImageBean;
		}
		return null;
	}
	
	public DisplayImageOptions getDisplayImageOptions(){
		
		if(mOptions != null){
			return mOptions;
		}
		return null;
	}
	

	public void onButtonSelected(int id) {
		switch (id) {
		
		case R.id.btn_next:
			
			if(Index < arrListImageBean.size())
			{ 	
				Index++;
				mPager.setCurrentItem(Index);
			}
				
			break;
			
		case R.id.btn_prv:
			if(Index > 0){
				Index --;
				mPager.setCurrentItem(Index);
			}
			
			break;
		case R.id.btn_back:
			setResult(RESULT_OK,new Intent().putExtra(Const.INTENT_POSITION,Index+""));
			finish();
			break;
	

		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	
		if (keyCode == KeyEvent.KEYCODE_MENU) {
	    	PerformMenu();
	    	return true; //always eat it!	    	
	    }
	    else if(keyCode == KeyEvent.KEYCODE_BACK)
	    {
	    	if (mCustomTopMenu.isShowing()) {
	    		mCustomTopMenu.hide();
	    		if(mCustomBottomBar.isShowing()){
		    		mCustomBottomBar.hide();
		    	}

	    		setResult(RESULT_OK,new Intent().putExtra(Const.INTENT_POSITION,Index+""));
				return true;
	    	}
	    	
	    }
		
		return super.onKeyDown(keyCode, event); 
	}
	
	private  void PerformMenu() {
		
		if(mCustomTopMenu.isShowing()){
			mCustomTopMenu.hide();
		}else{
			
			findViewById(R.id.main_layout).post(new Runnable() {
				   public void run() {
					   mCustomTopMenu.show(findViewById(R.id.main_layout),Index,arrListImageBean);
				   }
				});
			
		}
		
		if(mCustomBottomBar.isShowing()){
			mCustomBottomBar.hide();
		}else{
			
			findViewById(R.id.main_layout).post(new Runnable() {
				   public void run() {
					   mCustomBottomBar.show(findViewById(R.id.main_layout),Index,arrListImageBean);
				   }
				});
			
		}
		
	}
	private void hidingMenu(){

		if(mCustomTopMenu.isShowing()){
			mCustomTopMenu.hide();
		}
		if(mCustomBottomBar.isShowing()){
			mCustomBottomBar.hide();
		}
	}

	public void onClick(View v) {
		PerformMenu();
		
	}
	 
	
	
	 
}
